package org.churchillroadpta.csvparser;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;


public class CsvParserManager {
    
    
    private CSVParser createParser(String filename) throws IOException {

        CSVLexer lex = new CSVLexer(new ANTLRFileStream(filename));
        CommonTokenStream tokens = new CommonTokenStream(lex);
        CSVParser parser = new CSVParser(tokens);
        return parser;
    }
    

	public List<List<String>> parse(InputStream inputStream) throws IOException {
		
        List<List<String>> result = null;
	
		CSVLexer lex = new CSVLexer(new ANTLRInputStream(inputStream));
        CommonTokenStream tokens = new CommonTokenStream(lex);
        CSVParser parser = new CSVParser(tokens);
        
        try {
            result = (List<List<String>>) parser.csvfile();
        }
        catch (RecognitionException e) {
            e.printStackTrace();
        }
        
        return result;
	}
	
    public List<List<String>> parse(String filename) throws IOException {

        List<List<String>> result = null;
        CSVParser parser = createParser(filename);
        
        try {
            result = (List<List<String>>) parser.csvfile();
        }
        catch (RecognitionException e) {
            e.printStackTrace();
        }
        
        return result;
    }

}
